package com.ruoyi.bantong.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.bantong.domain.BtRedbagPoll;
import com.ruoyi.bantong.mapper.BtRedbagBlacklistMapper;
import com.ruoyi.bantong.mapper.BtRedbagPollMapper;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.bantong.mapper.BtRedbagPollDetailsMapper;
import com.ruoyi.bantong.domain.BtRedbagPollDetails;
import com.ruoyi.bantong.service.IBtRedbagPollDetailsService;

/**
 * 红包玩法详情Service业务层处理
 * 
 * @author laohei
 * @date 2021-02-06
 */
@Service
public class BtRedbagPollDetailsServiceImpl implements IBtRedbagPollDetailsService 
{
    @Autowired
    private BtRedbagPollDetailsMapper btRedbagPollDetailsMapper;

    @Autowired
    private BtRedbagPollMapper pollMapper;

    @Autowired
    private BtRedbagBlacklistMapper blacklistMapper;

    /**
     * 查询红包玩法详情
     * 
     * @param rpdId 红包玩法详情ID
     * @return 红包玩法详情
     */
    @Override
    public BtRedbagPollDetails selectBtRedbagPollDetailsById(Integer rpdId)
    {
        return btRedbagPollDetailsMapper.selectBtRedbagPollDetailsById(rpdId);
    }

    /**
     * 查询红包玩法详情列表
     * 
     * @param btRedbagPollDetails 红包玩法详情
     * @return 红包玩法详情
     */
    @Override
    public List<BtRedbagPollDetails> selectBtRedbagPollDetailsList(BtRedbagPollDetails btRedbagPollDetails)
    {
        return btRedbagPollDetailsMapper.selectBtRedbagPollDetailsList(btRedbagPollDetails);
    }

    /**
     * 查询红包玩法中奖列表 前太使用
     *
     * @param btRedbagPollDetails 红包玩法详情
     * @return 红包玩法详情集合
     */
    @Override
    public List<Map<String, Object>> selectBtRedbagWinList(BtRedbagPollDetails btRedbagPollDetails) {
        return btRedbagPollDetailsMapper.selectBtRedbagWinList(btRedbagPollDetails);
    }

    /**
     * 新增红包玩法详情
     * 
     * @param btRedbagPollDetails 红包玩法详情
     * @return 结果
     */
    @Override
    public int insertBtRedbagPollDetails(BtRedbagPollDetails btRedbagPollDetails)
    {
        btRedbagPollDetails.setCreateTime(DateUtils.getNowDate());
        return btRedbagPollDetailsMapper.insertBtRedbagPollDetails(btRedbagPollDetails);
    }

    /**
     * 修改红包玩法详情
     * 
     * @param btRedbagPollDetails 红包玩法详情
     * @return 结果
     */
    @Override
    public int updateBtRedbagPollDetails(BtRedbagPollDetails btRedbagPollDetails)
    {
        Integer result = btRedbagPollDetails.getResult();
        BtRedbagPollDetails details = btRedbagPollDetailsMapper.selectBtRedbagPollDetailsById(btRedbagPollDetails.getRpdId());
        Integer winConut = btRedbagPollDetailsMapper.selectWinCountByRpId(details.getRpId());
        BtRedbagPoll btRedbagPoll = pollMapper.selectBtRedbagPollById(details.getRpId());
        Integer winNum = btRedbagPoll.getWinNum();
        Integer count = blacklistMapper.selectBlackListByUser(details.getUserId(), btRedbagPoll.getType());
        if(count > 0){
            throw new RuntimeException("该用户在黑名单中！");
        }
        if(result == 1 && (winConut + 1) > winNum){
            throw new RuntimeException("中奖人数超过规定人数");
        }
        btRedbagPollDetails.setUpdateTime(DateUtils.getNowDate());
        return btRedbagPollDetailsMapper.updateBtRedbagPollDetails(btRedbagPollDetails);
    }

    /**
     * 批量删除红包玩法详情
     * 
     * @param rpdIds 需要删除的红包玩法详情ID
     * @return 结果
     */
    @Override
    public int deleteBtRedbagPollDetailsByIds(Integer[] rpdIds)
    {
        return btRedbagPollDetailsMapper.deleteBtRedbagPollDetailsByIds(rpdIds);
    }

    /**
     * 删除红包玩法详情信息
     * 
     * @param rpdId 红包玩法详情ID
     * @return 结果
     */
    @Override
    public int deleteBtRedbagPollDetailsById(Integer rpdId)
    {
        return btRedbagPollDetailsMapper.deleteBtRedbagPollDetailsById(rpdId);
    }
}
